package com.yurun.loginmodule.controller;

import com.yurun.commonapi.api.CommonResult;
import com.yurun.commonapi.pojo.Lesson;
import com.yurun.commonapi.pojo.LessonSet;
import com.yurun.loginmodule.util.JWTUtil;
import com.yurun.orglessonmodule.service.LessonService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.web.util.WebUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.ServletRequest;
import java.math.BigInteger;
import java.util.Date;

@Api(tags = "课程模块")
@RestController
@RequestMapping("/lessonManage")
public class LessonController {

    @Autowired
    private LessonService lessonService;

    @ApiOperation("根据条件查询课程信息")
    @RequiresPermissions("/allLessonInfo")
    @RequestMapping("/allLessonInfo")
    public CommonResult allLessonInfo(ServletRequest request, String branchId, String keyword, int pageNum, int pageSize) {
        // 获取token
        String token = WebUtils.toHttp(request).getHeader("Authorization").substring(6);
        // 获取机构id
        String organizationId = JWTUtil.getOrganizationId(token);
        return lessonService.allLessonInfo(BigInteger.valueOf(Long.parseLong(organizationId)), BigInteger.valueOf(Long.parseLong(branchId)), keyword, pageNum, pageSize);
    }

    @ApiOperation("增加课程")
    @RequiresPermissions("/addLesson")
    @RequestMapping("/addLesson")
    public CommonResult addLesson(ServletRequest request, Lesson lesson) {
        // 获取token
        String token = WebUtils.toHttp(request).getHeader("Authorization").substring(6);
        // 获取机构id
        String organizationId = JWTUtil.getOrganizationId(token);
        // 获取分店id
        String branchId = JWTUtil.getBranchId(token);

        lesson.setOrganizationId(BigInteger.valueOf(Long.parseLong(organizationId)));
        lesson.setBranchId(BigInteger.valueOf(Long.parseLong(branchId)));
        lesson.setCreateTime(new Date());
        lesson.setUpdateTime(new Date());
        lesson.setIsDel(0);
        return lessonService.addLesson(lesson);
    }

    @ApiOperation(value = "获取某一条课程信息")
    @RequiresPermissions("/getOneLessonInfo")
    @RequestMapping("/getOneLessonInfo")
    public CommonResult getOneLessonInfo(BigInteger id) {
        return lessonService.getOneLessonInfo(id);
    }

    @ApiOperation(value = "修改课程信息")
    @RequiresPermissions("/editLesson")
    @RequestMapping("/editLesson")
    public CommonResult editLesson(ServletRequest request, Lesson lesson) {
        // 获取token
        String token = WebUtils.toHttp(request).getHeader("Authorization").substring(6);
        // 获取机构id
        String organizationId = JWTUtil.getOrganizationId(token);
        // 获取分店id
        String branchId = JWTUtil.getBranchId(token);
        lesson.setOrganizationId(BigInteger.valueOf(Long.parseLong(organizationId)));
        lesson.setBranchId(BigInteger.valueOf(Long.parseLong(branchId)));
        lesson.setUpdateTime(new Date());
        return lessonService.editLesson(lesson);
    }

    @ApiOperation(value = "删除课程")
    @RequiresPermissions("/delLesson")
    @RequestMapping("/delLesson")
    public CommonResult delLesson(BigInteger id) {
        return lessonService.delLesson(id);
    }

    @ApiOperation(value = "获取所有班级名")
    @RequestMapping("/getClassroomOption")
    public CommonResult getClassroomOption(ServletRequest request, BigInteger branchId){
        // 获取token
        String token = WebUtils.toHttp(request).getHeader("Authorization").substring(6);
        // 获取机构id
        String organizationId = JWTUtil.getOrganizationId(token);
        //暂时从token中获取branchid
        String branchIdtoken = JWTUtil.getBranchId(token);
//        System.out.println("获取token中的机构id："+organizationId);
//        System.out.println("获取token中的分店id："+JWTUtil.getBranchId(token));
//        System.out.println("前端传来的分店id是: "+branchId);
//        return lessonService.getClassroomOption(BigInteger.valueOf(Long.parseLong(organizationId)), branchId);
        return lessonService.getClassroomOption(BigInteger.valueOf(Long.parseLong(organizationId)), BigInteger.valueOf(Long.parseLong(branchIdtoken)));
    }

    @ApiOperation(value = "获取所有学年")
    @RequestMapping("/getSchoolYearOption")
    public CommonResult getSchoolYearOption(ServletRequest request, BigInteger classroomId, BigInteger branchId) {
        // 获取token
        String token = WebUtils.toHttp(request).getHeader("Authorization").substring(6);
        // 获取机构id
        String organizationId = JWTUtil.getOrganizationId(token);

        return lessonService.getSchoolYearOption(BigInteger.valueOf(Long.parseLong(organizationId)),classroomId, branchId);
    }

    @ApiOperation(value = "获取所有学期")
    @RequestMapping("/getTermOption")
    public CommonResult getTermOption(ServletRequest request, BigInteger classroomId, BigInteger branchId, String schoolYear) {
        // 获取token
        String token = WebUtils.toHttp(request).getHeader("Authorization").substring(6);
        // 获取机构id
        String organizationId = JWTUtil.getOrganizationId(token);

        return lessonService.getTermOption(BigInteger.valueOf(Long.parseLong(organizationId)),classroomId, branchId, schoolYear);

    }

    @ApiOperation(value = "设置课程表")
    @RequiresPermissions("/addLessonSet")
    @RequestMapping("/addLessonSet")
    public CommonResult addLessonSet(ServletRequest request, LessonSet lessonSet) {
        // 获取token
        String token = WebUtils.toHttp(request).getHeader("Authorization").substring(6);
        // 获取机构id
        String organizationId = JWTUtil.getOrganizationId(token);
        // 获取分店id
        String branchId = JWTUtil.getBranchId(token);
        lessonSet.setOrganizationId(BigInteger.valueOf(Long.parseLong(organizationId)));
        lessonSet.setBranchId(BigInteger.valueOf(Long.parseLong(branchId)));
        return lessonService.addLessonSet(lessonSet);
    }

    @ApiOperation(value = "获取某个课程表")
    @RequiresPermissions("/getOneTimetable")
    @RequestMapping("/getOneTimetable")
    public CommonResult getOneTimetable(ServletRequest request, LessonSet lessonSet) {
        // 获取token
        String token = WebUtils.toHttp(request).getHeader("Authorization").substring(6);
        // 获取机构id
        String organizationId = JWTUtil.getOrganizationId(token);
        lessonSet.setOrganizationId(BigInteger.valueOf(Long.parseLong(organizationId)));
        return lessonService.getOneTimetable(lessonSet);
    }

    @ApiOperation(value = "获取所有课程名")
    @RequestMapping("/getLessonOption")
    public CommonResult getLessonOption(ServletRequest request) {
        // 获取token
        String token = WebUtils.toHttp(request).getHeader("Authorization").substring(6);
        // 获取机构id
        String organizationId = JWTUtil.getOrganizationId(token);
        // 获取分店id
        String branchId = JWTUtil.getBranchId(token);
        return lessonService.getLessonOption(BigInteger.valueOf(Long.parseLong(organizationId)), BigInteger.valueOf(Long.parseLong(branchId)));
    }

    @ApiOperation(value = "根据课程名获取所有教师名")
    @RequestMapping("/getTeacherOptionByLessonName")
    public CommonResult getTeacherOptionByLessonName(ServletRequest request, Lesson lesson) {
        // 获取token
        String token = WebUtils.toHttp(request).getHeader("Authorization").substring(6);
        // 获取机构id
        String organizationId = JWTUtil.getOrganizationId(token);
        // 获取分店id
        String branchId = JWTUtil.getBranchId(token);
        lesson.setOrganizationId(BigInteger.valueOf(Long.parseLong(organizationId)));
        lesson.setBranchId(BigInteger.valueOf(Long.parseLong(branchId)));
        return lessonService.getTeacherOptionByLessonName(lesson);
    }

    @ApiOperation(value = "修改课程表")
    @RequiresPermissions("/editLessonSet")
    @RequestMapping("/editLessonSet")
    public CommonResult editLessonSet(ServletRequest request, LessonSet lessonSet) {
        // 获取token
        String token = WebUtils.toHttp(request).getHeader("Authorization").substring(6);
        // 获取机构id
        String organizationId = JWTUtil.getOrganizationId(token);

        lessonSet.setOrganizationId(BigInteger.valueOf(Long.parseLong(organizationId)));
        lessonSet.setUpdateTime(new Date());
        return lessonService.editLessonSet(lessonSet);
    }


}
